package com.member.model;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

@WebServlet("/MemDBLookup")
public class MemDBLookup extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public MemDBLookup() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		 Connection con = null;
		    
		    res.setContentType("text/html; charset=Big5");
		    PrintWriter out = res.getWriter();
		    try {
		      
		    	Context ctx = new javax.naming.InitialContext();
				DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/ProjectDB");
					con = ds.getConnection();
		     
		      out.println("<HTML><HEAD><TITLE>Member information</TITLE></HEAD>");
		      out.println("<BODY>");
		      out.println("<UL>");
		     /* while(rs.next()) {
		        out.println("<LI>" + rs.getString("name") + " " + rs.getString("phone"));
		      }
		    */
		     
		      out.println(new HtmlSQLResult("SELECT * FROM MEMBR", con));
		      
		      
		      
		      out.println("</UL>");
		      out.println("</BODY></HTML>");
		    } catch(Exception e) {
		      out.println("SQLException caught: " + e.getMessage());
		    }
		    finally {
		      // Always close the database connection.
		      try {
		        if (con != null) con.close();
		      }
		      catch (SQLException ignored) { }
		    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  doGet(request, response);
	}

}
